package com.runner.entity;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class PhysicsComponent implements Constants {

	protected Body body;
	protected BodyDef bodyDef;
	protected FixtureDef bodyFixture;
	protected TYPE type;
	protected float direction;
	protected float scale;

	public PhysicsComponent(Vector2 position, TYPE t, String path, boolean isStatic, boolean sensor, Sprite sprite, CollisionEntity entity) {
		this(position, t, path, isStatic, sensor, true, 1, sprite, entity);
	}

	public PhysicsComponent(Vector2 position, TYPE t, String path, boolean isStatic, boolean sensor, boolean gravitational, float scale,
			Sprite sprite, CollisionEntity entity) {
		this(1, 1, position, t, path, isStatic, sensor, gravitational, scale, sprite, entity);
	}

	public PhysicsComponent(float cols, float rows, Vector2 position, TYPE t, String path, boolean isStatic, boolean sensor, boolean gravitational,
			float scale, Sprite sprite, CollisionEntity entity) {
		type = t;
		direction = 0;
		this.scale = scale;
		bodyDef = new BodyDef();
		bodyFixture = new FixtureDef();
		if (!gravitational) {
			bodyDef.gravityScale = 0;
		}
		if (!isStatic) {
			bodyDef.type = BodyType.DynamicBody;
		} else {
			bodyDef.type = BodyType.StaticBody;
		}
		bodyDef.position.set(new Vector2(position.x + sprite.getWidth() / cols * SCALE * scale * Locator.getxSpriteScale() / 2, position.y
				+ sprite.getHeight() / rows * SCALE * scale * Locator.getySpriteScale() / 2));
		bodyDef.fixedRotation = true;

		body = Locator.getWorld().createBody(bodyDef);

		PolygonShape shape = new PolygonShape();
		shape.setAsBox(sprite.getWidth() / cols / 2 * SCALE * this.scale * Locator.getxSpriteScale(), sprite.getHeight() / rows / 2 * SCALE
				* this.scale * Locator.getySpriteScale());
		bodyFixture.shape = shape;
		if (sensor) {
			bodyFixture.isSensor = true;
		}
		body.setLinearDamping(8.0f);

		body.createFixture(bodyFixture);
		body.setUserData(entity);
	}

	public void update(float delta) {
		body.setTransform(body.getPosition(), direction*MathUtils.degreesToRadians);
	}

	public void delete() {
		Locator.getWorld().destroyBody(body);
		body = null;
	}

	public Body getBody() {
		return body;
	}

	public void setBody(Body body) {
		this.body = body;
	}

	public FixtureDef getBodyFixture() {
		return bodyFixture;
	}

	public void setBodyFixture(FixtureDef bodyFixture) {
		this.bodyFixture = bodyFixture;
	}

	public TYPE getType() {
		return type;
	}

	public void setType(TYPE type) {
		this.type = type;
	}

	public void setPosition(Vector2 position, Sprite sprite) {
		body.setTransform(position.x + sprite.getWidth() * SCALE * scale * Locator.getxSpriteScale() / 2, position.y + sprite.getHeight() * SCALE
				* scale * Locator.getySpriteScale() / 2, 0);

	}
}
